package com.example.administrator.guangdon2021_1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nle.mylibrary.forUse.zigbee.FourChannelValConvert;
import com.nle.mylibrary.forUse.zigbee.Zigbee;
import com.nle.mylibrary.transfer.DataBusFactory;

public class MainActivity extends AppCompatActivity {
    private TextView tv_hum,tv_temp;
    private Button btn_open;
    private Zigbee zigbee;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        Mythread mythread=new Mythread();
        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mythread.start();
            }
        });
    }
    private void init(){
        tv_hum=findViewById(R.id.tv_hum);
        tv_temp=findViewById(R.id.tv_temp);
        btn_open=findViewById(R.id.btn_open);
        zigbee=new Zigbee(DataBusFactory.newSocketDataBus("172.18.14.200",4196));


    }
    private class  Mythread extends  Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    double[] fourEnter = zigbee.getFourEnter();
                    double temperature = FourChannelValConvert.getTemperature(fourEnter[0]);
                    double humidity = FourChannelValConvert.getHumidity(fourEnter[1]);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_hum.setText(humidity+" RH");
                            tv_temp.setText(temperature+" ℃");
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
